#define _CRT_SECURE_NO_WARNINGS 1

https://leetcode.cn/problems/balanced-binary-tree/description/

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:

    int getDepth(TreeNode* root)
    {
        if (root == NULL)
            return 0;

        int leftHeight = getDepth(root->left);
        if (leftHeight == -1)
            return -1;

        int rightHeight = getDepth(root->right);
        if (rightHeight == -1)
            return -1;

        if (abs(leftHeight - rightHeight) > 1)
            return -1;
        else
            return max(leftHeight, rightHeight) + 1;
    }

    bool isBalanced(TreeNode* root) {

        if (getDepth(root) == -1)
            return false;
        else
            return true;
    }
};